package news.dao.empl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import news.dao.DaoFactory;
import news.dao.UserDao;
import news.entity.User;

public class UserDaoImpl implements UserDao {
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	public boolean userRegister(User user) {
		conn=DaoFactory.getConn();
		boolean flag=false;
		try {
			String sql="insert into tb_user values(user_seq.nextval,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, user.getUsername());
			pstmt.setString(2, user.getUserpwd());
			
			int a=pstmt.executeUpdate();//操作成功的行数
			if(a>0){
				flag=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	
	public User userLogin(String username, String userpwd) {
		conn=DaoFactory.getConn();
		User user=null;
		try {
			String sql="select * from tb_user where username=? and userpwd=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, userpwd);
			
			rs=pstmt.executeQuery();			
			if(rs.next()){
				user=new User();
				user.setUsername(rs.getString("username"));
				user.setUserpwd(rs.getString("userpwd"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

}
